import os
import shutil

# shutil.copy(r'D:\BaiduNetdiskDownload\bdd100k_labels\bdd100k\labels\100k\train', r'F:\yolov5_master_yyf\data')
# shutil.copy(r'D:\BaiduNetdiskDownload\bdd100k_labels\bdd100k\labels\100k\val', r'F:\yolov5_master_yyf\data')



# images_path = r'F:\yolov5_master_yyf\data\JPEGimages'
#
# img_n_list = []  # 图片名字不加.jpg后缀
# images_names = os.listdir(images_path)
# images_num = len(images_names)
# for i in range(images_num):
#     images_name = images_names[i].split('.')[0]
#     img_n_list.append(images_name)
#
# # print(img_n_list)
# print(f"图片数量：{len(img_n_list)}")
#
# lab_path = r"D:\BaiduNetdiskDownload\bdd100k_labels\bdd100k\labels\100k\val"
# t_lab_path = r"F:\yolov5_master_yyf\data\labels"
#
# # 如果存在labels，删除文件夹；如果不存在，创建文件夹
# if os.path.exists(t_lab_path):
#     shutil.rmtree(t_lab_path)
#     os.mkdir(t_lab_path)
# else:
#     os.mkdir(t_lab_path)
#
# lab_n_list = []
# # 100k下有两个文件，train和val，分别改路径
# lab_name = os.listdir(lab_path)
# labels_num = len(lab_name)
# print(f"总标签数量：{labels_num}")
#
# for j in range(labels_num):
#     label_name = lab_name[j].split('.')[0]
#     if label_name in img_n_list: # 如果标签名和图片名相同
#         lab_n_list.append(lab_name)
#
# print(f"处理后标签数量：{len(lab_n_list)}")
#         # # 复制文件
#         # shutil.copy(f"{lab_path}/{label_name}.json", t_lab_path)
#         # print(f"{lab_path}/{label_name}.json 移动成功！")

if __name__ == '__main__':
    Labels_path = r"F:\Bdd100k-tiny\JPEGimg"

    lab_n_list = []  # 标签名字不加.txt后缀
    label_names = os.listdir(Labels_path)
    label_num = len(label_names)
    for i in label_names:
        label_name = i.split('.')[0]
        lab_n_list.append(label_name)

    img_path = r"D:\BaiduNetdiskDownload\bdd100k_images\bdd100k\images\100k\train"

    count = 0
    img_names = os.listdir(img_path)
    for j in img_names:
        img_name = j.split('.')[0]
        if img_name in lab_n_list:
            ori_path = img_path + f"/{img_name}.jpg"
            obj_path = r"F:\Bdd100k-tiny\JPEGimg"
            shutil.copy(ori_path, obj_path)
            count += 1
            print(f"num:{count}")
            print(f"{img_name}复制成功")
